package com.vcc.web.core.util;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.vcc.core.exception.ProgramException;
import com.vcc.core.exception.SignInException;
import com.vcc.data.entity.Role;
import com.vcc.data.entity.User;
import org.apache.commons.lang3.StringUtils;

import java.util.Optional;

public class UserUtil {

    private static final String CUSTOMER_SESSION_KEY = "USER";

    public static void set(User customer, Role role) {
        JSONObject map = new JSONObject();
        map.put("id", customer.getId());
        map.put("roleId", role.getId());
        map.put("user", customer);
        map.put("role", role);
        StpUtil.getSession().set(CUSTOMER_SESSION_KEY, map);
    }

    public static User get() {
        JSONObject map = (JSONObject) StpUtil.getSession().get(CUSTOMER_SESSION_KEY);
        if (map == null) {
            throw new SignInException("用户未登录");
        }
        User user = (User) map.get("user");
        if (user == null) {
            throw new SignInException("用户未登录");
        }
        return user;
    }

    public static Role getRole() {
        JSONObject map = (JSONObject) StpUtil.getSession().get(CUSTOMER_SESSION_KEY);
        if (map == null) {
            throw new SignInException("用户未登录");
        }
        Role role = (Role) map.get("role");
        if (role == null) {
            throw new SignInException("用户未登录");
        }
        return role;
    }

    public static Boolean hasParent() {
        User user = get();
        return user.getParentId() == null || user.getParentId() == 0L;
    }

    public static Boolean hasShowAllData() {
        Role role = getRole();
        return role.getHasAllData();
    }

    public static Boolean hasShowAllData(Role role) {
        return role.getHasAllData();
    }

    public static Long getId() {
        JSONObject map = (JSONObject) StpUtil.getSession().get(CUSTOMER_SESSION_KEY);
        if (map == null) {
            throw new SignInException("用户未登录");
        }
        return Optional.of(map.getLongValue("id")).orElseThrow(() -> new ProgramException("用户未登录"));
    }

    public static Long getIdNotNull() {
        JSONObject map = (JSONObject) StpUtil.getSession().get(CUSTOMER_SESSION_KEY);
        if (map == null) {
            throw new SignInException("用户未登录");
        }
        return map.getLongValue("id");
    }

    public static void login(Long id, String device) {
        if (StpUtil.isDisable(id)) {
            throw new SignInException("账号被封禁");
        }
        if (StringUtils.isNotBlank(device)) {
            StpUtil.login(id, device);
        } else {
            StpUtil.login(id);
        }
    }

    public static void logout() {
        StpUtil.logout();
    }

    public static void logout(Long loginId) {
        StpUtil.logout(loginId);
    }

    public static void openSafe(Long safeTime) {
        StpUtil.openSafe(safeTime);
    }

    public static void openSafe(String service, Long safeTime) {
        StpUtil.openSafe(service, safeTime);
    }

    public static void kickout(Long id) {
        StpUtil.kickout(id);
    }


    public static void baseVerifyUser(
            Role role,
            Long userId,
            Long targetUserId,
            Long parentId,
            Long targetUserParentId
    ) {
        parentId = parentId == null ? 0L : parentId;
        if (role.getHasAllData()) {
            //获取卡片所属经销商
            // dealerId 是 0 则是主账户，非0 则是子账户
            //userCardDealerId == 0 则是主账户的卡，非0 则是子账户的卡
            if (parentId != 0L) {
                // 如果卡不是主账户的也不是主账户名下用户的责非法操作
                if (
                        !targetUserId.equals(userId) &&
                                !targetUserId.equals(parentId) &&
                                !targetUserParentId.equals(parentId)
                ) {
                    throw new ProgramException("非法操作");
                }
            } else {
                //主账户在操作
                //主账户时 判断卡的所属主账户是否等于操作用户和卡所属用户是否等于操作用户
                if (!targetUserParentId.equals(userId) && !targetUserId.equals(userId)) {
                    throw new ProgramException("非法操作");
                }
            }
        } else {
            if (!userId.equals(targetUserId)) {
                throw new ProgramException("非法操作");
            }
        }
    }

}
